package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.GameDefine;
	import engine.logic.SceneTransfer;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneChangeManager;
	import engine.scene.SceneManager;
	import engine.scene.player.BasePlayerModel;
	import engine.ui.UIManage;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;

	/**
	 * 
	 * @author gaoyq
	 * 
	 */
	public class RightNoticeLogic
	{
		public static var _self:RightNoticeLogic;
		private var _model:ModelLocator;
		private var _showArr  :Array = [];
		private var _timeoutIndex:int = 0;
		private var uiCatchList:Array = [];
		private var indexUI:RightNoticeUI;
		public function RightNoticeLogic()
		{
			uiinit();
		}
		
		public static function getInstance():RightNoticeLogic
		{
			if(_self == null)
			{
				_self =new RightNoticeLogic();
			}
			return _self;
		}
		
		public function uiinit():void
		{
			_model = ModelLocator.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SHOW_NEWS),addNoticeUI);
//			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ACTIVITY_STATE), onShow);
//			_model.addEventListener(SceneEvent.SceneLoaded,onLoaded);
//			_model.addEventListener(SceneEvent.AttackSceneLoaded,onLoaded);
			
		}
		
		private function onShow (e:CustomMsgEvent):void
		{
			if(e.pList[0]==1)
			{
				showNotice(e.pList[1],2);
			}
		}
		
		public function addNoticeUI(e:CustomMsgEvent):void
		{
			if(PkListUI.getInstance().created || EscortSceneUI.getInstance().created) return;
			showNotice(e.pList[1],e.pList[0]);

		}
		/**
		 *装备装备 
		 * @param id type 2,3,1 ,4 5. 立即装备,活动开启,伙伴,礼包,经脉
		 * 
		 */
		public function showNotice(id:int,type:int):void
		{
			if(type == 4 && _showArr.indexOf(id)!=-1)
			{//
				return;
			}
			if(type == 3 && ActivitiesUI.getInstance().checkAfk(id) != 0)
			{//已经开始替身娃娃的活动不提示
				return;
			}
		
			if(uiCatchList.length>0)
			{
				indexUI = uiCatchList.shift() as RightNoticeUI;
			}else
			{
				indexUI = new RightNoticeUI();
				//右下角提示框界面
				UIManage.getInstance()._creating = 0;
				UIManage.getInstance().initUI(indexUI,"RightNoticeUI");
			}
			var obj:Object = StaticIni.getIniObj("rightNotice_" + type);
			switch (type)
			{
				case 2:
					indexUI.lab_name.htmlStr = StaticIni.getSwfLangStr(String(id)) ;
					indexUI.pic.source = ResManager.newResClass("icon_" +StaticIni.getIniObj(String(id)).Photo);//ResPath.ITEM +"icon_" +StaticIni.getIniObj(String(id)).Photo + ".jpg";
					//obj.transition
//					indexUI.lab_can.htmlStr	=	 "<font size='14'><a href='event:" + type + "," + id + "'>" +"立即装备" + "</a></font>";
					break;
				case 3:
					if(ActivitiesUI.getInstance().checkAfk(id) == 0)
					{
						indexUI.lab_name.htmlStr = StaticIni.getSwfLangStr("ActivityStr_"+id) ;
						var mc:MovieClip  =		ResManager.newResClass( StaticIni.getIniVar("action_"+id,"picName")) as MovieClip;
						if(mc == null)return
						if(mc["effect1"] != null)
						{
							mc["effect1"].stop();
							mc["effect1"].visible	=	false;
						}
						indexUI.pic.source = mc;
					}
//					indexUI.lab_can.htmlStr	=	 "<font size='14'><a href='event:" + type + "," + id + "'>" +"活动开启" + "</a></font>";
					break;
				case 1:
					indexUI.lab_name.htmlStr = StaticIni.getSwfLangStr(String(id));
					indexUI.pic.source = ResManager.newResClass("minihead_" + StaticIni.getIniVar(id,"Model"));
//					indexUI.pic.source = ResPath.PHOTO + "minihead_" + StaticIni.getIniVar(id,"Model") + ".png";
//					indexUI.lab_can.htmlStr	=	 "<font size='14'><a href='event:" + type + "," + id + "'>" +"邀请伙伴" + "</a></font>";
					break;
				case 4:
					indexUI.lab_name.htmlStr = StaticIni.getSwfLangStr(String(id)) ;
					indexUI.pic.source = ResManager.newResClass("icon_" +StaticIni.getIniObj(String(id)).Photo);// ResPath.ITEM +"icon_" +StaticIni.getIniObj(String(id)).Photo + ".jpg";
					//obj.transition
//					indexUI.lab_can.htmlStr	=	 "<font size='14'><a href='event:" + type + "," + id + "'>" +"打开礼包" + "</a></font>";
					break;
				case 5:
					if(PulseLogic.getInstance().ui.created)return;
					indexUI.lab_name.htmlStr = "";
					indexUI.pic.source = ResManager.newResClass("CustomControl_0189");
					break;
			}
			 
			indexUI.lab_can.addLinkClickHandel(onLinkClick);
			indexUI.lab_can.addEventListener(MouseEvent.CLICK,txtClickHandle);
			if(!obj.hasOwnProperty("info")) return;
//			indexUI.lab_info.htmlStr = StaticIni.getSwfLangStr(obj.info);
			indexUI.lab_can.htmlStr	=	 "<font size='14'><a href='event:" + type + "," + id + "'>" + StaticIni.getSwfLangStr(obj.btnlab) + "</a></font>";
			indexUI.addEventListener(MouseEvent.MOUSE_OVER,onUIOverHandle);
			indexUI.addEventListener(MouseEvent.MOUSE_OUT,onUIoutHandle);
			
			indexUI.layout = "right";
			indexUI.initPosX = 20;
			indexUI.initPosY = 400;
			indexUI.alpha = 1;
			checkIsShow()
			indexUI.show();
			indexUI.parent.setChildIndex(indexUI,0);
			_showArr.unshift(indexUI);
			
			moveView();
			
			if(GameData.SceneType	!=	GameDefine.SceneType_Normal) indexUI.visible =	false;//setVisible(false);
		}
		public function checkIsShow():Boolean
		{
			var b:Boolean=_showArr&&_showArr.length;
			if(b&&NpcTalkUI.getInstance().created)
			{
				NpcTalkUI.getInstance().showOrHide();
			}
			return b
		}
//		private function onLoaded(e:SceneEvent):void
//		{
//			if(_showArr.length != 0)
//			{
//				if(GameData.SceneType == GameDefine.SceneType_Normal)
//				{
//					setVisible(true);
//				}
//				else
//				{
//					setVisible(false);
//				}
//			}
//		}
		
		private function moveView():void
		{
			for(var i:int = 0;i<_showArr.length;i+=2)
			{
				var rightUI:RightNoticeUI = _showArr[i] as RightNoticeUI;
				var rightUI1:RightNoticeUI = _showArr[i+1] as RightNoticeUI;
				var moveX:int = SceneChangeManager.getGameWidth() - rightUI.width  + SceneChangeManager.getGameX();
				var moveX1:int = SceneChangeManager.getGameWidth() - rightUI.width*2+10 + SceneChangeManager.getGameX();
				TweenLite.to(rightUI,.4,{x:moveX,y:335-(i/2)*rightUI.bg.height });
				rightUI.initPosY = 400;
				rightUI.initPosX = 0;
				if(_showArr[i+1])
				{
					TweenLite.to(rightUI1,.4,{x:moveX1,y:335-(i/2)*rightUI1.bg.height });
					rightUI1.initPosY = 400;
					rightUI1.initPosX = 0;
				}
			}
		}
		
		private function onUIOverHandle(e:MouseEvent):void
		{
			clearTimeout(_timeoutIndex);
		}
		
		private function onUIoutHandle(e:MouseEvent):void
		{
			var ui:RightNoticeUI = e.currentTarget as RightNoticeUI;
			_timeoutIndex = setTimeout(
				function():void
				{
					TweenLite.to(ui,.5,{alpha:0,onComplete:
						
						function ():void
						{
							ui.hide();
						}
					})
				},int ( StaticIni.getIniVar("rightNoticeUtil","time") ) );
		}
		
		public function txtClickHandle(e:MouseEvent):void
		{
			e.currentTarget.parent.hide();
		}
		
		public function onLinkClick(val:String):void
		{
			var arr:Array = val.split(",");
			var obj:Object = StaticIni.getIniObj("rightNotice_" + arr[0]);
			// 1,2,3 伙伴,装备,活动
			switch (int(arr[0]))
			{
				case 1:
//					PartnerRequestLogic.getInstance().ui.showOrHide();
					//酒店老板ID 21000514
					var npcModel : * = SceneManager.getInstance().getConfigMC(obj["NPCId"]);
					if(npcModel != null)
					{
						if(FunctionGuideUI.getInstance().stage) return;
						npcModel.dispatchEvent( new MouseEvent( MouseEvent.MOUSE_DOWN ));
					}
					else 
					{
						SceneTransfer.getInstance().tranferTo(obj["transition"]);
					}	
					break;
				case 2:
				case 4:
					
					var view:View = _model.scene.GetView(View.VIEWPORT_TOOL);
					var newEquip:ViewObject;
					view.getDataForEach(function (key:String,data:*):void
					{
						if(data is ViewObject)
						{
							if(data.CheckProperties("ConfigID"))
							{
								if (data.GetProperties("ConfigID") == arr[1])
								{
									newEquip = data;
								}
							}
						}
					});
					if(newEquip == null)return;
					if(int(arr[0]) == 2)
					{
						PackLogic.getInstance().equip(newEquip);
						MyPropertyLogic.getInstance().ui.show();
					}else
					{
						PackLogic.getInstance().ui.show();
						SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_USEITEM,View.VIEWPORT_TOOL, int( newEquip.ObjKey ) );
					}
					
					break;
				case 3:
				    var actionObj:Object = StaticIni.getIniObj("action_"+arr[1]);
					SceneTransfer.getInstance().tranferTo(actionObj.transition);
					break;
				case 5:
					SkillLogic.getInstance().showUI();
					break;
					
			}
			if(indexUI)
				indexUI.hide();
//			SceneTransfer.getInstance().tranferTo(val);
		}
		
		public function setVisible(boolean:Boolean):void
		{
			if(indexUI)
			{
				var xPoint : int = SceneChangeManager.getGameWidth() - indexUI.width - 15 + SceneChangeManager.getGameX();
			}
			
			var length:int	=	_showArr.length;
			for(var i:int=0;i<length;i++)
			{
				(_showArr[i] as RightNoticeUI).visible	=	boolean;
				(_showArr[i] as RightNoticeUI).x = xPoint;
			}
		}
		
		public function changePoint():void
		{
			moveView();
//			var length:int	=	_showArr.length;
//			if( length == 0 ) return;
//			if(indexUI)
//			{
//				var xPoint : int = SceneChangeManager.getGameWidth() -  indexUI.width - 15 + SceneChangeManager.getGameX();
//			}
//
//			for(var i:int=0;i<length;i++)
//			{
//				(_showArr[i] as RightNoticeUI).x = xPoint;
//			}
		}
		
		private function onCloseClickHandle(e:MouseEvent):void
		{
			e.currentTarget.parent.hide();
		}
		
		public function BeforeShow(ui:RightNoticeUI):void
		{
//			_timeoutIndex = setTimeout(
//				function():void
//				{
//					TweenLite.to(ui,.5,{alpha:0,onComplete:
//					
//						function ():void
//						{
//							ui.hide();
//						}
//					})
//				},int ( StaticIni.getIniVar("rightNoticeUtil","time") ) );
			ui.closeBtn.addEventListener(MouseEvent.CLICK,onCloseClickHandle);
			addListenter();
		}
		
		
		public function AfterHide(ui:RightNoticeUI):void
		{
			if(ui.lab_can!=null)
			{
				_showArr.splice(_showArr.indexOf(ui),1);
				ui.pic.source = null;
				uiCatchList.unshift(ui);
			}
			ui.closeBtn.removeEventListener(MouseEvent.CLICK,onCloseClickHandle);
			removeListenter();
			moveView();
			
		}
		
		private function addListenter():void
		{
			
		}
		
		private function removeListenter():void
		{
			
		}
		
	}
}